package com.jinzo.mporaba.tasks;

import java.net.UnknownHostException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import com.jinzo.mporaba.Mporaba;
import com.jinzo.mporaba.QuotaItem;

import android.os.Handler;
//import android.util.Log;

public class LoadFromSOAPTask implements Runnable {
	private String username;
	private String password;
	private Handler callback;
	private ArrayList<QuotaItem> m_results= new ArrayList<QuotaItem>();
	
	public LoadFromSOAPTask() {
	}
	
	public void load(String username, String password, Handler callback) {
		this.username = username;
		this.password = password;
		this.callback = callback;
		
		Thread thread = new Thread(this);
		thread.setDaemon(true);
		thread.start();
	}
	public void run() {
		m_results.clear();
		Object result = new Object();
		try {
			result = this.getMonitorData(username, password);
		} catch (UnknownHostException uhe) { 
			//Log.e("ConnError", uhe.getMessage());
			callback.sendEmptyMessage(Mporaba.REQUEST_ERROR_CONNECTION);
			return;
		} catch (Exception e) {
			//Log.e("SoapError", e.getMessage());
			callback.sendEmptyMessage(Mporaba.REQUEST_ERROR_SOAP);
			return;
		}
		try {
			/* TODO: Fetch all */
			m_results.add(new QuotaItem("Moneta",((SoapObject)result).getProperty("Moneta").toString()));
			m_results.add(new QuotaItem("Poraba",((SoapObject)result).getProperty("Poraba").toString()));
			m_results.add(new QuotaItem("Stanje",((SoapObject)result).getProperty("Stanje").toString()));
			SoapObject quota = (SoapObject)((SoapObject)result).getProperty("Quotas");
			for (int i=0; i<quota.getPropertyCount(); i++) {
				SoapObject qs = (SoapObject)quota.getProperty(i);
        		m_results.add(new QuotaItem((qs.getProperty("Description")).toString(), true, (qs.getProperty("Unit")).toString(), (qs.getProperty("Available")).toString(), (qs.getProperty("Used")).toString()));
			} 
		} catch (NullPointerException npe) {
			//Log.e("SoapError", "NullPointerException has no msg.");
			callback.sendEmptyMessage(Mporaba.REQUEST_ERROR_LOGIN);
		}catch (Exception e) {
    		//Log.e("SoapParseError", e.getMessage());
    		callback.sendEmptyMessage(Mporaba.REQUEST_ERROR_SOAP);
		}	
		callback.sendEmptyMessage(Mporaba.REQUEST_SUCCESS);
	}
	
	public ArrayList<QuotaItem> getResult() {
		return m_results;
	}
	
	private Object getMonitorData(String uporabniskoIme, String geslo) throws Exception {
		final String SOAP_ACTION = "http://mobitel.si/MobiDesktop/Monitor";
	    final String METHOD_NAME = "Monitor";
	    final String NAMESPACE = "http://mobitel.si/MobiDesktop";
	    final String URL = "https://moj.mobitel.si/mobidesktop-v2/service";
	    
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("Username", uporabniskoIme);
		request.addProperty("Password", geslo);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);	
        androidHttpTransport.call(SOAP_ACTION, envelope);
        Object result = envelope.getResponse();
        return result;

	}
}
